use std::env;
use std::fs;
use std::io::{BufReader,BufRead};
use std::io::prelude::*;
use std::fs::File;

use gen;

//To Do: cannibalize check_files for command prompt file configuration functions
pub fn check_files(mut config_dir: String) {
    let mut rotor_dir:String = config_dir.clone();
    config_dir.push_str("daily.txt");
    if fs::metadata(&config_dir).is_ok() != true {
        println!("Path: '{}' does not exist. Generating now.", config_dir);
        let mut file = File::create(&config_dir).ok().expect("failed to open dailies file");
        let key = gen::keygen(config_dir);
        file.write_all(key.as_ref()).ok().expect("failed to write dailies file");

    }
    rotor_dir.push_str("rotors.txt");
    if fs::metadata(&rotor_dir).is_ok() != true {
        println!("Path: '{}' does not exist. Generating rotor configurations to {}.", rotor_dir, rotor_dir);
        let mut file = File::create(&rotor_dir.clone()).ok().expect("failed to open dailies file");
        let mut rotor = String::new();
        for _i in 0 .. 5 {
            rotor.push_str(&gen::rotorgen(rotor_dir.clone()));
        }
        file.write_all(rotor.as_ref()).ok().expect("failed to write dailies file");

    }

}


pub fn config_dir()->String {
    match env::home_dir() {
        Some(path) => {
            let mut dir = String::new();
            dir.push_str(path.to_str().unwrap());
            dir.push_str("/.config/rust-enigma/");
            fs::create_dir_all(&dir).expect("lol");
            dir.to_owned()
        },
        None => {
            println!("Impossible to get your home dir! Using current directory");
            fs::create_dir_all("rust-enigma").expect("Unable to create rust-enigma directory");
            "rust-enigma".to_owned()
        }
    }
}


pub fn load_dailies(mut config_dir: String) -> Vec<String> {
    config_dir.push_str("daily.txt");

    let file = File::open(&config_dir).unwrap();
    let mut dailies: Vec<String> = Vec::new();
    for line in BufReader::new(file).lines() {
        dailies.push(line.unwrap().trim().to_owned());
    }
    dailies
}

//use for lines one and two of dailies
pub fn parse_order(input: String) -> Vec<u8>{
    let output:Vec<u8> = input.trim().split_whitespace().map(|s| s.parse().unwrap())
        .collect();
    output
}


pub fn parse_indicator(input:String) -> Vec<u8> {
    let raw: Vec<char> = input.chars().collect();
    let mut output: Vec<u8> = Vec::new();
    for i in 0 .. raw.len() {
        output.push(raw[i] as u8 -65);
    }
    output

}


pub fn parse_pairs(input:String) -> Vec<u8> {  
    //parses plugboard and reflector pairs and returns a vector of u8
    //it may prove more useful to return a string, for simplicity
    let mut base: Vec<u8> = Vec::new();
    for i in 0..26 {
        base.push(i as u8);
    }
    //strip spaces
    let raw: Vec<char> = input.chars().filter(|&c| !" ".contains(c)).collect();
    let mut j = 0;
    //make connections on reflector/plugboard
    while j < raw.len() {
        let xy: (u8, u8)=(base[raw[j] as usize -65],base[raw[j+1] as usize -65]);
        base[raw[j+1] as usize -65] = xy.0;
        base[raw[j] as usize -65] = xy.1;

        j += 2;
    }
    base
}

pub fn load_rotors(mut config_dir: String) -> Vec<String> {
    config_dir.push_str("rotors.txt");
    let file = File::open(&config_dir).unwrap();
    let mut rotors: Vec<String> = Vec::new();
    for line in BufReader::new(file).lines() {
         rotors.push(line.unwrap().trim().to_owned());
    }
    rotors
    
}

